package in.muton.edushare.panels;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.FormComponentPanel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

public class MonthYearChooser extends FormComponentPanel<Date> {

	private static final long serialVersionUID = 3602411968014609004L;
	private Date date;
	private String selectedMonth;
	private int selectedYear;
	DropDownChoice<String> month;
	DropDownChoice<Integer> year;
	
	public static final ArrayList<String> monthList = new ArrayList<String>();
	static{
		monthList.add("January");
		monthList.add("February");
		monthList.add("March");
		monthList.add("April");
		monthList.add("May");
		monthList.add("June");
		monthList.add("July");
		monthList.add("August");
		monthList.add("September");
		monthList.add("October");
		monthList.add("November");
		monthList.add("December");
	}
	
	public MonthYearChooser(String id) {
		super(id);
		this.date = new Date();
		init();
	}

	public MonthYearChooser(String id, IModel<Date> model){
		super(id, model);
		if(model.getObject()==null)
			this.date = new Date();
		else if(!(model.getObject() instanceof Date))
				throw new WicketRuntimeException("MonthYearChooser["
						                            + getPath()
						                            + "] contains an invalid model object, must be an object of type java.util.Date");
		
		this.date = model.getObject();
		init();
	}

	private void init() {
		
		Model<String> mm = new Model<String>(){
			
			private static final long serialVersionUID = 7635155637037849399L;
			
			@Override
			public String getObject() {
				return selectedMonth;	
			};
			
			@Override
			public void setObject(String object) {
				selectedMonth = object;
			}
			
		};
		
		Model<Integer> my = new Model<Integer>(){
			
			private static final long serialVersionUID = 7635155637037849399L;
			
			@Override
			public Integer getObject() {
				return selectedYear;	
			};
			
			@Override
			public void setObject(Integer object) {
				selectedYear = object;
			}
		};
		
		month = new DropDownChoice<String>("month",mm, monthList);
		year = new DropDownChoice<Integer>("year",my, getYears());
		
		setDate();
		
		add(month);
		add(year);
		
	}
	
	private void setDate() {
		selectedMonth = new SimpleDateFormat("MMMM").format(date);
		selectedYear = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
	}

	@Override
	protected void convertInput() {

		java.sql.Date d = java.sql.Date.valueOf(""+year.getConvertedInput()+"-" +
												(monthList.indexOf(month.getConvertedInput())+1<10?"0":"")+
												(monthList.indexOf(month.getConvertedInput())+1)+
												"-01");
		date.setTime(d.getTime());
		setConvertedInput(date);
	}
	
	private ArrayList<Integer> getYears(){
		ArrayList<Integer> al = new ArrayList<Integer>();
		Calendar cal = Calendar.getInstance();

        for (int i = cal.get(Calendar.YEAR) - 59; i < cal.get(Calendar.YEAR) + 10; i++) {
            al.add(new Integer(i));
        }
        
        return al;
	}
}
